//
//  BPPhotographerViewController.m
//  BabyPhotograph
//
//  Created by cesh on 2017/2/8.
//  Copyright © 2017年 cesh. All rights reserved.
//

#import "BPPhotographerViewController.h"
#import "PlayerView.h"
#import "BPTimeChooseView.h"
#import "BPPicPreviewViewController.h"
#import "BPPhotographerCommentViewController.h"
#import "UILabel+BPBoundingRect.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
@interface BPPhotographerViewController () <UIScrollViewDelegate, BPTimeChooseViewProtocol>

@property (strong, nonatomic) UIScrollView *scrollV;
@property (strong, nonatomic) UIView *descriptionView;
@property (strong, nonatomic) CALayer *descriptionViewLayer;
@property (strong, nonatomic) UIImageView *backImg;

@property (assign, nonatomic) CGFloat topViewHeight;

@property (strong, nonatomic) UIView *selectedDateCell;

@property (strong, nonatomic) NSString *selectedDate;
@property (assign, nonatomic) NSInteger selectedDateIndex;
@property (strong, nonatomic) NSString *selectedTime;
@property (assign, nonatomic) NSInteger selectedTimeIndex;

@property (strong, nonatomic) BPTimeChooseView *innerLeftView;
@property (strong, nonatomic) BPTimeChooseView *innerMidView;
@property (strong, nonatomic) BPTimeChooseView *innerRightView;

@property (strong, nonatomic) UILabel *leftLabel;
@property (strong, nonatomic) UILabel *midLabel;
@property (strong, nonatomic) UILabel *rightLabel;

@property (strong, nonatomic) NSArray *photographerTimes;

@property (strong, nonatomic) NSDictionary *data;
@property (strong, nonatomic) NSArray *imgs;
@property (strong, nonatomic) NSArray *comments;



@end

@implementation BPPhotographerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0xec / 255.0 green:0xf5 / 255.0 blue:0xfc / 255.0 alpha:1.0];
    self.title = @"摄影师详情";

    self.automaticallyAdjustsScrollViewInsets = NO;
    //获取摄影师资料
    [self getPhographerData];
    
    //设置最外层scrollView
    [self setupContentScrollView];
    
    //底部选择按钮
    [self setupBtmBtm];
}

- (void)getPhographerData {
    [SVProgressHUD showWithStatus:@"读取中"];
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                     @"text/html",
                                                     @"image/jpeg",
                                                     @"image/png",
                                                     @"application/octet-stream",
                                                     @"text/json",
                                                     nil];
    
    [mgr POST:[NSString stringWithFormat:@"%@?c=Photographer&&a=infoByPid",requestURL]
   parameters:@{@"photographerID" : self.photographerID}
     progress:^(NSProgress * _Nonnull uploadProgress) {}
      success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
          if ([responseObject[@"state"] isEqualToString:@"SUCCESS"]) {
              self.data = responseObject[@"data"];
              NSLog(@"摄影师资料 %@", responseObject[@"data"]);
              //顶部的视频播放view
              [self setupVideoView];
              
              
          }
          else {
              [SVProgressHUD showWithStatus:@"读取错误"];
              dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                  [SVProgressHUD dismiss];
              });
          }
          
      }
      failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
          [SVProgressHUD showWithStatus:@"读取错误"];
          dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
              [SVProgressHUD dismiss];
          });
      }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.alpha = 0.01;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.alpha = 0.01;
}

//根据拖动多少实现navBar的隐藏
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.navigationController.navigationBar.hidden = scrollView.contentOffset.y == 0;
    self.navigationController.navigationBar.alpha = scrollView.contentOffset.y / 64.0 + 0.01;
}

- (void)setupContentScrollView {
    UIScrollView *scrollV = [[UIScrollView alloc]init];
    scrollV.delegate = self;
    self.scrollV = scrollV;
    scrollV.frame = CGRectMake(0, 0, screenW, screenH - screenW / 375.0 * 60);
    scrollV.contentSize = CGSizeMake(0, screenW / 375.0 * (self.topViewHeight + 165 + 172 + 154) + 40);
    scrollV.backgroundColor = [UIColor clearColor];
    [self.view addSubview:scrollV];
}

- (void)setupVideoView {
    
    NSArray *video = self.data[@"video"];
    //如果没有视频，添加一个图片
    if(video.count == 0){
         [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:self.data[@"img"][0]] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
             //处理下载进度
             NSLog(@"下载进度%li / %li",(long)receivedSize,(long)expectedSize);
         } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
             if (error) {
                 NSLog(@"图片下载错误 %@",error);
             }
             if (image) {
                 //图片下载完成  在这里进行相关操作，如加到数组里 或者显示在imageView上
                 NSLog(@"图片 %@", image);
                 //根据该图片的尺寸等比例放大
                 UIImageView *imgV = [[UIImageView alloc]init];
                 imgV.frame = CGRectMake(10, 0, screenW - 20, (screenW - 20) * image.size.height / image.size.width);
                 self.topViewHeight = imgV.frame.size.height;
                 imgV.image = image;
                 imgV.layer.cornerRadius = 8.0;
                 imgV.layer.masksToBounds = YES;
                 
                 [self.scrollV addSubview:imgV];
                 CALayer *layer = [CALayer layer];
                 layer.frame = CGRectMake(imgV.layer.frame.origin.x, imgV.layer.frame.origin.y, imgV.layer.frame.size.width, imgV.layer.frame.size.height);
                 layer.backgroundColor = [UIColor colorWithRed:0x76 / 255.0 green:0xb1 / 255.0 blue:0xfd / 255.0 alpha:0.8].CGColor;
                 layer.shadowColor = [UIColor colorWithRed:0x76 / 255.0 green:0xb1 / 255.0 blue:0xfd / 255.0 alpha:0.8].CGColor;
                 layer.shadowOffset = CGSizeMake(0, 2);
                 layer.shadowOpacity = 1.0;
                 layer.shadowRadius = 3;
                 layer.cornerRadius = 8;
                 [self.scrollV.layer insertSublayer:layer below:imgV.layer];
                 
                 
                 UIImageView *backImg = [[UIImageView alloc]init];
                 self.backImg = backImg;
                 backImg.frame = CGRectMake(14, 30, 40, 24);
                 backImg.image = [UIImage imageNamed:@"back_black"];
                 backImg.contentMode = UIViewContentModeLeft;
                 backImg.userInteractionEnabled = YES;
                 UITapGestureRecognizer *backGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pop)];
                 [backImg addGestureRecognizer:backGes];
                 [self.scrollV addSubview:backImg];
                 
                 //摄影师介绍的view
                 [self setupDescriptionView];
                 
                 //摄影师作品view
                 [self setupPhotosView];
                 
                 //摄影师评价
                 [self setupReviewsView];
             }
         }];
    
    }
    //有视频 ，加载视频
    else{
        PlayerView *playerView = [[PlayerView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenW / 375.0 * 210)];
        self.topViewHeight = playerView.frame.size.height;
        playerView.photoVC = self;
        //视频地址
        NSString *videoUrl = self.data[@"video"][0];
        playerView.url = [NSURL URLWithString:videoUrl];
        
        //返回按钮点击事件回调
        [playerView backButton:^(UIButton *button) {
            NSLog(@"返回按钮被点击");
        }];
        //播放完成回调
        [playerView endPlay:^{
            NSLog(@"播放完成");
        }];
        [self.scrollV addSubview:playerView];
        UIImageView *backImg = [[UIImageView alloc]init];
        self.backImg = backImg;
        backImg.frame = CGRectMake(14, 30, 40, 24);
        backImg.image = [UIImage imageNamed:@"back_black"];
        backImg.contentMode = UIViewContentModeLeft;
        backImg.userInteractionEnabled = YES;
        UITapGestureRecognizer *backGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pop)];
        [backImg addGestureRecognizer:backGes];
        [self.scrollV addSubview:backImg];
        
        //摄影师介绍的view
        [self setupDescriptionView];
        
        //摄影师作品view
        [self setupPhotosView];
        
        //摄影师评价
        [self setupReviewsView];
    }
    
}

- (void)pop {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setupDescriptionView {
    UIView *descriptionView = [[UIView alloc]init];
    
    UILabel *name = [[UILabel alloc] init];
    name.text = @"摄影师";
    name.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    name.textColor = [UIColor colorWithRed:185/255.0 green:198/255.0 blue:208/255.0 alpha:1/1.0];
    [name sizeToFit];
    name.origin = CGPointMake(10, screenW / 375.0 * 10);
    [descriptionView addSubview:name];
    
    UILabel *age = [[UILabel alloc] init];
    age.text = @"性别";
    age.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    age.textColor = [UIColor colorWithRed:185/255.0 green:198/255.0 blue:208/255.0 alpha:1/1.0];
    [age sizeToFit];
    age.origin = CGPointMake(10, name.bottom + screenW / 375.0 * 15);
    [descriptionView addSubview:age];
    
    UILabel *experience = [[UILabel alloc] init];
    experience.text = @"摄影经验";
    experience.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    experience.textColor = [UIColor colorWithRed:185/255.0 green:198/255.0 blue:208/255.0 alpha:1/1.0];
    [experience sizeToFit];
    experience.origin = CGPointMake(10, age.bottom + screenW / 375.0 * 15);
    [descriptionView addSubview:experience];
    
    UILabel *phone = [[UILabel alloc] init];
    phone.text = @"手机号";
    phone.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    phone.textColor = [UIColor colorWithRed:185/255.0 green:198/255.0 blue:208/255.0 alpha:1/1.0];
    [phone sizeToFit];
    phone.origin = CGPointMake(10, experience.bottom + screenW / 375.0 * 15);
    [descriptionView addSubview:phone];
    
    UILabel *star = [[UILabel alloc] init];
    star.text = @"星级";
    star.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    star.textColor = [UIColor colorWithRed:185/255.0 green:198/255.0 blue:208/255.0 alpha:1/1.0];
    [star sizeToFit];
    star.origin = CGPointMake(10, phone.bottom + screenW / 375.0 * 15);
    [descriptionView addSubview:star];
    
    UILabel *desc = [[UILabel alloc] init];
    desc.text = @"自我简介";
    desc.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    desc.textColor = [UIColor colorWithRed:185/255.0 green:198/255.0 blue:208/255.0 alpha:1/1.0];
    [desc sizeToFit];
    desc.origin = CGPointMake(10, star.bottom + screenW / 375.0 * 15);
    [descriptionView addSubview:desc];
    
    UILabel *name_detail = [[UILabel alloc] init];
    name_detail.text = self.data[@"name"];
    name_detail.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16];
    name_detail.textColor = [UIColor colorWithRed:118/255.0 green:177/255.0 blue:253/255.0 alpha:1/1.0];
    [name_detail sizeToFit];
    name_detail.origin = CGPointMake(72, screenW / 375.0 * 5);
    [descriptionView addSubview:name_detail];
    
    UILabel *age_detail = [[UILabel alloc] init];
    NSString *sex = [self.data[@"sex"] isEqualToString:@"0"] ? @"未定义" : [self.data[@"sex"] isEqualToString:@"1"] ? @"男" : @"女";
    age_detail.text = sex;
    age_detail.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16];
    age_detail.textColor = [UIColor colorWithRed:118/255.0 green:177/255.0 blue:253/255.0 alpha:1/1.0];
    [age_detail sizeToFit];
    age_detail.origin = CGPointMake(72, name_detail.bottom + screenW / 375.0 * 10);
    [descriptionView addSubview:age_detail];
    
    UILabel *experience_detail = [[UILabel alloc] init];
    experience_detail.text = [NSString stringWithFormat:@"%@年", self.data[@"experience"]];
    experience_detail.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16];
    experience_detail.textColor = [UIColor colorWithRed:118/255.0 green:177/255.0 blue:253/255.0 alpha:1/1.0];
    [experience_detail sizeToFit];
    experience_detail.origin = CGPointMake(72, age_detail.bottom + screenW / 375.0 * 10);
    [descriptionView addSubview:experience_detail];
    
    UILabel *phone_detail = [[UILabel alloc] init];
    phone_detail.text = self.data[@"phone"];
    phone_detail.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16];
    phone_detail.textColor = [UIColor colorWithRed:118/255.0 green:177/255.0 blue:253/255.0 alpha:1/1.0];
    [phone_detail sizeToFit];
    phone_detail.origin = CGPointMake(72, experience_detail.bottom + screenW / 375.0 * 10);
    [descriptionView addSubview:phone_detail];
    
    
    UIImageView *star1 = [[UIImageView alloc]init];
    UIImageView *star2 = [[UIImageView alloc]init];
    UIImageView *star3 = [[UIImageView alloc]init];
    UIImageView *star4 = [[UIImageView alloc]init];
    UIImageView *star5 = [[UIImageView alloc]init];
    
    star1.frame = CGRectMake(72, star.y, 15.0, 15.0);
    star2.frame = CGRectMake(star1.right + 3, star.y, 15.0, 15.0);
    star3.frame = CGRectMake(star2.right + 3, star.y, 15.0, 15.0);
    star4.frame = CGRectMake(star3.right + 3, star.y, 15.0, 15.0);
    star5.frame = CGRectMake(star4.right + 3, star.y, 15.0, 15.0);
    
    NSInteger starCount = [self.data[@"star_level"] integerValue];
    
    switch (starCount) {
        case 1:
        {
            star1.image = [UIImage imageNamed:@"star_s"];
            star2.image = [UIImage imageNamed:@"star_n"];
            star3.image = [UIImage imageNamed:@"star_n"];
            star4.image = [UIImage imageNamed:@"star_n"];
            star5.image = [UIImage imageNamed:@"star_n"];
        }
            break;
        case 2:
        {
            star1.image = [UIImage imageNamed:@"star_s"];
            star2.image = [UIImage imageNamed:@"star_s"];
            star3.image = [UIImage imageNamed:@"star_n"];
            star4.image = [UIImage imageNamed:@"star_n"];
            star5.image = [UIImage imageNamed:@"star_n"];
        }
            break;
        case 3:
        {
            star1.image = [UIImage imageNamed:@"star_s"];
            star2.image = [UIImage imageNamed:@"star_s"];
            star3.image = [UIImage imageNamed:@"star_s"];
            star4.image = [UIImage imageNamed:@"star_n"];
            star5.image = [UIImage imageNamed:@"star_n"];
        }
            break;
        case 4:
        {
            star1.image = [UIImage imageNamed:@"star_s"];
            star2.image = [UIImage imageNamed:@"star_s"];
            star3.image = [UIImage imageNamed:@"star_s"];
            star4.image = [UIImage imageNamed:@"star_s"];
            star5.image = [UIImage imageNamed:@"star_n"];
        }
            break;
        case 5:
        {
            star1.image = [UIImage imageNamed:@"star_s"];
            star2.image = [UIImage imageNamed:@"star_s"];
            star3.image = [UIImage imageNamed:@"star_s"];
            star4.image = [UIImage imageNamed:@"star_s"];
            star5.image = [UIImage imageNamed:@"star_s"];
        }
            break;
        default:
            break;
    }
    
    [descriptionView addSubview:star1];
    [descriptionView addSubview:star2];
    [descriptionView addSubview:star3];
    [descriptionView addSubview:star4];
    [descriptionView addSubview:star5];
    
    //建立一个2排字高度的scrollView
    UILabel *desc_detail = [[UILabel alloc] init];
    desc_detail.text = self.data[@"desc"];
    desc_detail.font = [UIFont fontWithName:@"PingFangSC-Medium" size:12];
    desc_detail.textColor = [UIColor colorWithRed:118/255.0 green:177/255.0 blue:253/255.0 alpha:1/1.0];
    [desc_detail sizeToFit];
    //取得一排字的高度
    CGFloat rowHeight = desc.height;
    UIScrollView *descScroolView = [[UIScrollView alloc]initWithFrame:CGRectMake(72, star.bottom + screenW / 375.0 * 12, screenW - phone_detail.x - 25, rowHeight * 3)];
    
    desc_detail.origin = CGPointMake(0, 0);
    [descScroolView addSubview:desc_detail];
    
    desc_detail.frame = CGRectMake(0, 0, descScroolView.width, 0);
    
    desc_detail.numberOfLines = 0; //PS：这句很主要，否则默认行数为1，只显示一行文字后面截断了就没有了
    CGRect myRect = [desc_detail boundingRectWithInitSize:desc_detail.frame.size];
    desc_detail.frame = CGRectMake(0, 0, descScroolView.width, myRect.size.height);
    
    descScroolView.contentSize = CGSizeMake(0, desc_detail.height);
    
    [descriptionView addSubview:descScroolView];
    
    
    descriptionView.frame = CGRectMake(10, self.topViewHeight + 10, screenW - 20, descScroolView.bottom + 10);
    descriptionView.backgroundColor = [UIColor whiteColor];
    descriptionView.layer.cornerRadius = 8.0;
    descriptionView.layer.masksToBounds = YES;
    self.descriptionView = descriptionView;
    
    //添加阴影
    CALayer *layer = [CALayer layer];
    self.descriptionViewLayer = layer;
    layer.frame = CGRectMake(descriptionView.layer.frame.origin.x, descriptionView.layer.frame.origin.y, descriptionView.layer.frame.size.width, descriptionView.layer.frame.size.height);
    layer.backgroundColor = [UIColor colorWithRed:0x76 / 255.0 green:0xb1 / 255.0 blue:0xfd / 255.0 alpha:0.8].CGColor;
    layer.shadowColor = [UIColor colorWithRed:0x76 / 255.0 green:0xb1 / 255.0 blue:0xfd / 255.0 alpha:0.8].CGColor;
    layer.shadowOffset = CGSizeMake(0, 2);
    layer.shadowOpacity = 1.0;
    layer.shadowRadius = 3;
    layer.cornerRadius = 8;
    [self.scrollV.layer insertSublayer:layer below:descriptionView.layer];
    
    [self.scrollV addSubview:descriptionView];
    
}

- (void)hidePhotographerV:(BOOL)hide {
    self.descriptionView.hidden = hide;
    self.descriptionViewLayer.hidden = hide;
    self.backImg.hidden = hide;
}

- (void)setupPhotosView {
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                     @"text/html",
                                                     @"image/jpeg",
                                                     @"image/png",
                                                     @"application/octet-stream",
                                                     @"text/json",
                                                     nil];
    
    [mgr POST:[NSString stringWithFormat:@"%@?c=Photographer&&a=worksByPid",requestURL]
   parameters:@{@"photographerID" : self.photographerID,
                @"page" : @"1",
                @"pageSize" : @"10"}
     progress:^(NSProgress * _Nonnull uploadProgress) {}
      success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
          if ([responseObject[@"state"] isEqualToString:@"SUCCESS"]) {
              NSLog(@"摄影师作品 %@", responseObject);
              NSMutableArray *imgs = [NSMutableArray array];
              NSArray *dataImgs = responseObject[@"data"][@"img"];
              
              for (NSDictionary *dict in dataImgs) {
                  [imgs addObject:dict[@"url"]];
              }
              
              self.imgs = imgs;
              UIView *photosView = [[UIView alloc]initWithFrame:CGRectMake(10, self.topViewHeight + 10 + self.descriptionView.height + 10, screenW - 20, screenW / 375.0 * 172)];
              photosView.backgroundColor = [UIColor whiteColor];
              photosView.layer.cornerRadius = 8.0;
              photosView.layer.masksToBounds = YES;
              //添加阴影
              CALayer *layer = [CALayer layer];
              layer.frame = CGRectMake(photosView.layer.frame.origin.x, photosView.layer.frame.origin.y, photosView.layer.frame.size.width, photosView.layer.frame.size.height);
              layer.backgroundColor = [UIColor colorWithRed:0x76 / 255.0 green:0xb1 / 255.0 blue:0xfd / 255.0 alpha:0.8].CGColor;
              layer.shadowColor = [UIColor colorWithRed:0x76 / 255.0 green:0xb1 / 255.0 blue:0xfd / 255.0 alpha:0.8].CGColor;
              layer.shadowOffset = CGSizeMake(0, 2);
              layer.shadowOpacity = 1.0;
              layer.shadowRadius = 3;
              layer.cornerRadius = 8;
              [self.scrollV.layer insertSublayer:layer below:photosView.layer];
              
              //添加顶栏
              UIView *blueV = [[UIView alloc]init];
              blueV.frame = CGRectMake(0, 8, 6, 22);
              blueV.backgroundColor = [UIColor colorWithRed:0x76 / 255.0 green:0xb1 / 255.0 blue:0xfd / 255.0 alpha:0.8];
              [photosView addSubview:blueV];
              
              UILabel *commentLbl = [[UILabel alloc]init];
              commentLbl.text = @"他的作品";
              commentLbl.textColor = [UIColor colorWithRed:0x76 / 255.0 green:0xb1 / 255.0 blue:0xfd / 255.0 alpha:0.8];
              commentLbl.font = [UIFont boldSystemFontOfSize:screenW / 375.0 * 16.0];
              [commentLbl sizeToFit];
              commentLbl.origin = CGPointMake(10, 10);
              [photosView addSubview:commentLbl];
              
              UILabel *viewMoreLbl = [[UILabel alloc]init];
              viewMoreLbl.text = @"查看更多 >";
              viewMoreLbl.font = [UIFont systemFontOfSize:screenW / 375.0 * 12.0];
              viewMoreLbl.textColor = [UIColor colorWithRed:0xb9 / 255.0 green:0xc6 / 255.0 blue:0xd0 / 255.0 alpha:0.8];
              [viewMoreLbl sizeToFit];
              viewMoreLbl.origin = CGPointMake(photosView.width - 10 - viewMoreLbl.width, 13);
              [photosView addSubview:viewMoreLbl];
              if (imgs.count >= 2) {
                  //添加两张图片
                  UIImageView *img1 = [[UIImageView alloc]init];
                  img1.frame = CGRectMake(10, blueV.bottom + screenW / 375.0 * 12, (photosView.width - 30) * 0.5, screenW / 375.0 * 120);
                  img1.contentMode = UIViewContentModeScaleAspectFill;
                  img1.image = [UIImage imageNamed:@"zhanweitu"];
                  if(imgs[0]){

                      SDWebImageManager   *manager   =   [SDWebImageManager   sharedManager];
                      
                      [manager   downloadImageWithURL:[NSURL URLWithString:imgs[0]]   options:SDWebImageRetryFailed   progress:^(NSInteger   receivedSize,   NSInteger   expectedSize)   {
                          
                      }   completed:^(UIImage   *image,   NSError   *error,   SDImageCacheType   cacheType,   BOOL   finished,   NSURL   *imageURL)   {
                          
                          img1.image = image;
                      }];
                      
                  }
                  img1.layer.masksToBounds = YES;
                  img1.layer.cornerRadius = 8.0;
                  [photosView addSubview:img1];
                  
                  UIImageView *img2 = [[UIImageView alloc]init];
                  img2.frame = CGRectMake(img1.right + 10, blueV.bottom + screenW / 375.0 * 12, (photosView.width - 30) * 0.5, screenW / 375.0 * 120);
                  img2.contentMode = UIViewContentModeScaleAspectFill;
                  img2.image = [UIImage imageNamed:@"zhanweitu"];
                  if (imgs[1]) {
                      SDWebImageManager   *manager   =   [SDWebImageManager   sharedManager];
                      
                      [manager   downloadImageWithURL:[NSURL URLWithString:imgs[1]]   options:SDWebImageRetryFailed   progress:^(NSInteger   receivedSize,   NSInteger   expectedSize)   {
                          
                      }   completed:^(UIImage   *image,   NSError   *error,   SDImageCacheType   cacheType,   BOOL   finished,   NSURL   *imageURL)   {
                          
                          img2.image = image;
                      }];
                  }
                  img2.layer.masksToBounds = YES;
                  img2.layer.cornerRadius = 8.0;
                  [photosView addSubview:img2];
              }
              
              [photosView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushPicPreviewVC)]];
              
              [self.scrollV addSubview:photosView];
          }
      } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
          
      }];
}

- (void)pushPicPreviewVC {
    BPPicPreviewViewController *picPreviewVC = [[BPPicPreviewViewController alloc]init];
    picPreviewVC.imgs = self.imgs;
    [self.navigationController pushViewController:picPreviewVC animated:YES];
}

- (void)setupReviewsView {
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                     @"text/html",
                                                     @"image/jpeg",
                                                     @"image/png",
                                                     @"application/octet-stream",
                                                     @"text/json",
                                                     nil];
    
    [mgr POST:[NSString stringWithFormat:@"%@?c=Photographer&&a=evaluate",requestURL]
   parameters:@{@"photographerID" : self.photographerID,
                @"page" : @"1",
                @"pageSize" : @"10"}
     progress:^(NSProgress * _Nonnull uploadProgress) {
         
     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         if ([responseObject[@"state"] isEqualToString:@"SUCCESS"]) {
             [SVProgressHUD dismiss];
             NSLog(@"摄影师评价 %@", responseObject[@"data"]);
             NSArray *comments = responseObject[@"data"];
             self.comments = comments;
             
             UIView *commentView = [[UIView alloc]init];
             
             //添加顶栏
             UIView *blueV = [[UIView alloc]init];
             blueV.frame = CGRectMake(0, 8, 6, 22);
             blueV.backgroundColor = [UIColor colorWithRed:0x76 / 255.0 green:0xb1 / 255.0 blue:0xfd / 255.0 alpha:0.8];
             [commentView addSubview:blueV];
             
             UILabel *commentLbl = [[UILabel alloc]init];
             commentLbl.text = @"评价";
             commentLbl.textColor = [UIColor colorWithRed:0x76 / 255.0 green:0xb1 / 255.0 blue:0xfd / 255.0 alpha:0.8];
             commentLbl.font = [UIFont boldSystemFontOfSize:screenW / 375.0 * 16.0];
             [commentLbl sizeToFit];
             commentLbl.origin = CGPointMake(10, 10);
             [commentView addSubview:commentLbl];
             
             UILabel *viewMoreLbl = [[UILabel alloc]init];
             viewMoreLbl.text = @"查看更多 >";
             viewMoreLbl.font = [UIFont systemFontOfSize:screenW / 375.0 * 12.0];
             viewMoreLbl.textColor = [UIColor colorWithRed:0xb9 / 255.0 green:0xc6 / 255.0 blue:0xd0 / 255.0 alpha:0.8];
             [viewMoreLbl sizeToFit];
             viewMoreLbl.origin = CGPointMake(commentView.width - 10 - viewMoreLbl.width, 13);
             [commentView addSubview:viewMoreLbl];
             
             NSInteger commentCount = self.comments.count >= 2 ? 2 : self.comments.count;
             
             switch (commentCount) {
                     //没有评论
                 case 0:
                 {
                     UIView *noCommentView = [[UIView alloc]initWithFrame:CGRectMake(0, 40, screenW - 20, 60)];
                     noCommentView.backgroundColor = [UIColor whiteColor];
                     
                     UILabel *noCommentLbl = [[UILabel alloc]init];
                     noCommentLbl.text = @"暂无评论";
                     noCommentLbl.font = [UIFont fontWithName:@"PingFangSC-Regular" size:screenW / 375.0 * 16.0];
                     noCommentLbl.textColor = [UIColor colorWithRed:139/255.0 green:163/255.0 blue:182/255.0 alpha:1/1.0];
                     [noCommentLbl sizeToFit];
                     noCommentLbl.center = CGPointMake(noCommentView.frame.size.width * 0.5, noCommentView.frame.size.height * 0.5);
                     [noCommentView addSubview:noCommentLbl];
                     
                     [commentView addSubview:noCommentView];
                     
                     commentView.frame = CGRectMake(10, self.topViewHeight + 10 + self.descriptionView.height + 10 + screenW / 375.0 * 172 + 10, screenW - 20, 35 + 60);
                 }
                     break;
                     //一条评论
                 case 1:
                 {
                     //第一条评论
                     UIImageView *commentHeader1 = [[UIImageView alloc]init];
                     commentHeader1.frame = CGRectMake(10, 40, 30, 30);
                     commentHeader1.layer.cornerRadius = 15.0;
                     commentHeader1.layer.masksToBounds = YES;
                     [commentHeader1 sd_setImageWithURL:[NSURL URLWithString:self.comments[0][@"user"][@"imgs"][0]] placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
                     //            commentHeader1.image = [UIImage imageNamed:@"combo_0"];
                     [commentView addSubview:commentHeader1];
                     UILabel *commentName1 = [[UILabel alloc]init];
                     commentName1.text = self.comments[0][@"user"][@"nickname"];
                     //            commentName1.text = @"用户1";
                     commentName1.textColor = [UIColor colorWithRed:0x8d / 255.0 green:0xa5 / 255.0 blue:0xb8 / 255.0 alpha:1.0];
                     commentName1.font = [UIFont systemFontOfSize:14.0];
                     [commentName1 sizeToFit];
                     commentName1.origin = CGPointMake(45, 45);
                     [commentView addSubview:commentName1];
                     UILabel *commentContent1 = [[UILabel alloc]init];
                     commentContent1.text = self.comments[0][@"content"];
                     //            commentContent1.text = @"用户1的介绍";
                     commentContent1.textColor = [UIColor colorWithRed:0xb9 / 255.0 green:0xc6 / 255.0 blue:0xd0 / 255.0 alpha:1.0];
                     commentContent1.font = [UIFont systemFontOfSize:14.0];
                     [commentContent1 sizeToFit];
                     commentContent1.origin = CGPointMake(45, 70);
                     [commentView addSubview:commentContent1];
                     
                     [commentView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushPhotographerCommentsVC)]];
                     commentView.frame = CGRectMake(10, self.topViewHeight + 10 + self.descriptionView.height + 10 + screenW / 375.0 * 172 + 10, screenW - 20, 35 + 60);
                     
                 }
                     break;
                     //两条评论
                 case 2:
                 {
                     //第一条评论
                     UIImageView *commentHeader1 = [[UIImageView alloc]init];
                     commentHeader1.frame = CGRectMake(10, 40, 30, 30);
                     commentHeader1.layer.cornerRadius = 15.0;
                     commentHeader1.layer.masksToBounds = YES;
                     [commentHeader1 sd_setImageWithURL:[NSURL URLWithString:self.comments[0][@"user"][@"imgs"][0]] placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
                     //            commentHeader1.image = [UIImage imageNamed:@"combo_0"];
                     [commentView addSubview:commentHeader1];
                     UILabel *commentName1 = [[UILabel alloc]init];
                     commentName1.text = self.comments[0][@"user"][@"nickname"];
                     //            commentName1.text = @"用户1";
                     commentName1.textColor = [UIColor colorWithRed:0x8d / 255.0 green:0xa5 / 255.0 blue:0xb8 / 255.0 alpha:1.0];
                     commentName1.font = [UIFont systemFontOfSize:14.0];
                     [commentName1 sizeToFit];
                     commentName1.origin = CGPointMake(45, 45);
                     [commentView addSubview:commentName1];
                     UILabel *commentContent1 = [[UILabel alloc]init];
                     commentContent1.text = self.comments[0][@"content"];
                     //            commentContent1.text = @"用户1的介绍";
                     commentContent1.textColor = [UIColor colorWithRed:0xb9 / 255.0 green:0xc6 / 255.0 blue:0xd0 / 255.0 alpha:1.0];
                     commentContent1.font = [UIFont systemFontOfSize:14.0];
                     [commentContent1 sizeToFit];
                     commentContent1.origin = CGPointMake(45, 70);
                     [commentView addSubview:commentContent1];
                     
                     //分割线
                     UIView *separator = [[UIView alloc]init];
                     separator.backgroundColor = [UIColor colorWithRed:0xd8 / 255.0 green:0xd8 / 255.0 blue:0xd8 / 255.0 alpha:1.0];
                     separator.frame = CGRectMake(10, 95, commentView.frame.size.width - 10, 0.5);
                     [commentView addSubview:separator];
                     //第2条评论
                     UIImageView *commentHeader2 = [[UIImageView alloc]init];
                     commentHeader2.frame = CGRectMake(10, 100, 30, 30);
                     [commentHeader2 sd_setImageWithURL:[NSURL URLWithString:self.comments[0][@"user"][@"imgs"][0]] placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
                     //            commentHeader2.image = [UIImage imageNamed:@"combo_1"];
                     commentHeader2.layer.cornerRadius = 15.0;
                     commentHeader2.layer.masksToBounds = YES;
                     [commentView addSubview:commentHeader2];
                     UILabel *commentName2 = [[UILabel alloc]init];
                     commentName2.text = self.comments[0][@"user"][@"nickname"];
                     //            commentName2.text = @"用户2";
                     commentName2.textColor = [UIColor colorWithRed:0x8d / 255.0 green:0xa5 / 255.0 blue:0xb8 / 255.0 alpha:1.0];
                     commentName2.font = [UIFont systemFontOfSize:14.0];
                     [commentName2 sizeToFit];
                     commentName2.origin = CGPointMake(45, 105);
                     [commentView addSubview:commentName2];
                     UILabel *commentContent2 = [[UILabel alloc]init];
                     commentContent2.text = self.comments[0][@"content"];
                     //            commentContent2.text = @"用户2的介绍";
                     commentContent2.textColor = [UIColor colorWithRed:0xb9 / 255.0 green:0xc6 / 255.0 blue:0xd0 / 255.0 alpha:1.0];
                     commentContent2.font = [UIFont systemFontOfSize:14.0];
                     [commentContent2 sizeToFit];
                     commentContent2.origin = CGPointMake(45, 130);
                     [commentView addSubview:commentContent2];
                     [commentView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushPhotographerCommentsVC)]];
                     
                     commentView.frame = CGRectMake(10, self.topViewHeight + 10 + self.descriptionView.height + 10 + screenW / 375.0 * 172 + 10, screenW - 20, 35 + 2 * 60);
                 }
                     break;
                     
                 default:
                     break;
             }
             
             commentView.backgroundColor = [UIColor whiteColor];
             commentView.layer.cornerRadius = 8.0;
             commentView.layer.masksToBounds = YES;
             //添加阴影
             CALayer *layer = [CALayer layer];
             layer.frame = CGRectMake(commentView.layer.frame.origin.x, commentView.layer.frame.origin.y, commentView.layer.frame.size.width, commentView.layer.frame.size.height);
             layer.backgroundColor = [UIColor colorWithRed:0x76 / 255.0 green:0xb1 / 255.0 blue:0xfd / 255.0 alpha:0.8].CGColor;
             layer.shadowColor = [UIColor colorWithRed:0x76 / 255.0 green:0xb1 / 255.0 blue:0xfd / 255.0 alpha:0.8].CGColor;
             layer.shadowOffset = CGSizeMake(0, 2);
             layer.shadowOpacity = 1.0;
             layer.shadowRadius = 3;
             layer.cornerRadius = 8;
             [self.scrollV.layer insertSublayer:layer below:commentView.layer];
             
             
             [self.scrollV addSubview:commentView];
             
             self.scrollV.contentSize = CGSizeMake(0, screenW / 375.0 * 172 + commentView.height + self.topViewHeight + self.descriptionView.height + 40);
             [self.scrollV layoutIfNeeded];
         }
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         
     }];
}

- (void)pushPhotographerCommentsVC {
    BPPhotographerCommentViewController *commentVC = [[BPPhotographerCommentViewController alloc]init];
    commentVC.data = self.comments;
    [self.navigationController pushViewController:commentVC animated:YES];
}

- (void)setupBtmBtm {
    UIView *btmV = [[UIView alloc]initWithFrame:CGRectMake(0, screenH - screenW / 375.0 * 60, screenW, screenW / 375.0 * 60)];
    btmV.backgroundColor = [UIColor whiteColor];
    UIView *topline = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, 0.5)];
    topline.backgroundColor = [UIColor colorWithRed:0xd8 / 255.0 green:0xd8 / 255.0 blue:0xd8 / 255.0 alpha:1.0];
    [btmV addSubview:topline];
    UIButton *button = [[UIButton alloc]init];
    button.size = CGSizeMake(btmV.width - 20, btmV.height - 20);
    button.center = CGPointMake(btmV.width * 0.5, btmV.height * 0.5);
    button.backgroundColor = [UIColor colorWithRed:118/255.0 green:177/255.0 blue:253/255.0 alpha:1/1.0];
    button.layer.cornerRadius = 4.0;
    [button setTitle:@"选择该摄影师" forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont fontWithName:@"PingFangSC-Medium" size:16]];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btmV addSubview:button];
    
    //给选择该摄影师按钮添加事件
    [button addTarget:self action:@selector(clickPhotographer) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btmV];
}

- (void)cancelChooseTime:(UIButton *)button {
    UIView *bgView = [[button superview]superview];
    [bgView removeFromSuperview];
}

- (void)tapOnDateCellView:(UITapGestureRecognizer *)tap {
    
    for (UIView *view in tap.view.subviews) {
        if ([view isKindOfClass:[UILabel class]]) {
            UILabel *label = (UILabel *)view;
            if (label.tag == 10) {
                self.selectedDate = label.text;
            }
        }
    }
    
    for (UIView *view in self.selectedDateCell.subviews) {
        if ([view isKindOfClass:[UILabel class]]) {
            UILabel *label = (UILabel *)view;
            label.textColor = [UIColor colorWithRed:141/255.0 green:165/255.0 blue:184/255.0 alpha:1/1.0];
            
        }
        else {
            [view removeFromSuperview];
        }
    }
    
    NSLog(@"%@", self.selectedDate);
    UIView *dateCellView = tap.view;
    
    //根据tag获取点击dateCellView的index
    self.selectedDateIndex = dateCellView.tag;
    
    if ([self.photographerTimes[dateCellView.tag][@"state"] integerValue] == 0) {
        self.innerLeftView.timeChooseType = TimeChooseTypeUnchooseable;
        self.innerMidView.timeChooseType = TimeChooseTypeUnchooseable;
        self.innerRightView.timeChooseType = TimeChooseTypeUnchooseable;
    }
    else {
        NSArray *times = self.photographerTimes[dateCellView.tag][@"times"];
        NSDateFormatter *format=[[NSDateFormatter alloc]init];
        [format setDateFormat:@"HH:mm"];
        //第一个时间段
        NSDate *startTime1 = [NSDate dateWithTimeIntervalSince1970:[times[0][@"s_time"] doubleValue]];
        NSString *startTime1String = [format stringFromDate:startTime1];
        
        NSDate *endTime1 = [NSDate dateWithTimeIntervalSince1970:[times[0][@"e_time"] doubleValue]];
        NSString *endTime1String = [format stringFromDate:endTime1];
        
        self.leftLabel.text = [NSString stringWithFormat:@"%@\n -- \n%@", startTime1String, endTime1String];
        //第二个时间段
        NSDate *startTime2 = [NSDate dateWithTimeIntervalSince1970:[times[1][@"s_time"] doubleValue]];
        NSString *startTime2String = [format stringFromDate:startTime2];
        
        NSDate *endTime2 = [NSDate dateWithTimeIntervalSince1970:[times[1][@"e_time"] doubleValue]];
        NSString *endTime2String = [format stringFromDate:endTime2];
        
        self.midLabel.text = [NSString stringWithFormat:@"%@\n -- \n%@", startTime2String, endTime2String];
        //第三个时间段
        NSDate *startTime3 = [NSDate dateWithTimeIntervalSince1970:[times[2][@"s_time"] doubleValue]];
        NSString *startTime3String = [format stringFromDate:startTime3];
        
        NSDate *endTime3 = [NSDate dateWithTimeIntervalSince1970:[times[2][@"e_time"] doubleValue]];
        NSString *endTime3String = [format stringFromDate:endTime3];
        
        self.rightLabel.text = [NSString stringWithFormat:@"%@\n -- \n%@", startTime3String, endTime3String];
        
        self.innerLeftView.timeChooseType = [times[0][@"state"] integerValue] == 0 ? TimeChooseTypeUnchooseable : TimeChooseTypeChooseable;
        self.innerMidView.timeChooseType = [times[1][@"state"] integerValue] == 0 ? TimeChooseTypeUnchooseable : TimeChooseTypeChooseable;
        self.innerRightView.timeChooseType = [times[2][@"state"] integerValue] == 0 ? TimeChooseTypeUnchooseable : TimeChooseTypeChooseable;
    }
    
    for (UILabel *label in dateCellView.subviews) {
        label.textColor = [UIColor colorWithRed:118/255.0 green:177/255.0 blue:253/255.0 alpha:1/1.0];
    }
    
    UIView *bottomView = [[UIView alloc]init];
    bottomView.frame = CGRectMake(0, dateCellView.frame.size.height - 3, dateCellView.frame.size.width, 3);
    bottomView.backgroundColor = [UIColor colorWithRed:118/255.0 green:177/255.0 blue:253/255.0 alpha:1/1.0];
    [dateCellView addSubview:bottomView];
    self.selectedDateCell = dateCellView;
}

- (void)clickPhotographer {
    [SVProgressHUD showWithStatus:@"读取中"];
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                     @"text/html",
                                                     @"image/jpeg",
                                                     @"image/png",
                                                     @"application/octet-stream",
                                                     @"text/json",
                                                     nil];
    
    [mgr POST:[NSString stringWithFormat:@"%@?c=Photographer&&a=workDay",requestURL]
   parameters:@{@"photographerID" : self.photographerID} progress:^(NSProgress * _Nonnull uploadProgress) {
       
   } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       if ([responseObject[@"state"] isEqualToString:@"SUCCESS"]) {
           NSArray *data = responseObject[@"data"];
           NSLog(@"摄影师时间 %@", data);
           self.photographerTimes = data;
           [SVProgressHUD dismiss];
           
           //弹出半透明黑色背景
           UIView *bgView = [[UIView alloc]init];
           bgView.frame = [UIScreen mainScreen].bounds;
           bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
           //下方的白色view
           UIView *chooseTimeView = [[UIView alloc]init];
           chooseTimeView.frame =CGRectMake(0, screenH - screenW / 375.0 * 260, screenW, screenW / 375.0 * 260);
           chooseTimeView.backgroundColor = [UIColor whiteColor];
           [bgView addSubview:chooseTimeView];
           //顶部的“选择服务时间”Label 和 X按钮
           UILabel *topLabel = [[UILabel alloc]init];
           topLabel.text = @"选择服务时间";
           topLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16];
           topLabel.textColor = [UIColor colorWithRed:141/255.0 green:165/255.0 blue:184/255.0 alpha:1/1.0];
           [topLabel sizeToFit];
           topLabel.center = CGPointMake(screenW * 0.5, screenW / 375.0 * 14 + topLabel.frame.size.height * 0.5);
           [chooseTimeView addSubview:topLabel];
           
           UIButton *cancelBtn = [[UIButton alloc]init];
           [cancelBtn setBackgroundImage:[UIImage imageNamed:@"cancel"] forState:UIControlStateNormal];
           [cancelBtn sizeToFit];
           cancelBtn.center = CGPointMake(screenW - screenW / 375.0 * 21 - cancelBtn.frame.size.width * 0.5, topLabel.center.y);
           [cancelBtn addTarget:self action:@selector(cancelChooseTime:) forControlEvents:UIControlEventTouchUpInside];
           
           [chooseTimeView addSubview:cancelBtn];
           //日期拖动条和日历按钮
           UIView *dateView = [[UIView alloc]init];
           dateView.backgroundColor = [UIColor colorWithRed:236/255.0 green:245/255.0 blue:252/255.0 alpha:1/1.0];
           dateView.frame = CGRectMake(0, screenW / 375.0 * 50, screenW, screenW / 375.0 * 52);
           [chooseTimeView addSubview:dateView];
           
           UIScrollView *dateScrollView = [[UIScrollView alloc]init];
           dateScrollView.showsHorizontalScrollIndicator = NO;
           dateScrollView.backgroundColor = [UIColor clearColor];
           dateScrollView.frame = CGRectMake(0, 0, screenW , dateView.frame.size.height);
           dateScrollView.contentSize = CGSizeMake(data.count * screenW / 375.0 * 83, 0);
           
           //三个时间段的选择的view
           UIView *leftView = [[UIView alloc]init];
           leftView.frame = CGRectMake(screenW / 375.0 * 10, dateView.bottom + screenW / 375.0 * 8, (screenW - screenW / 375.0 * 10 * 4) * 0.33, screenW / 375.0 * 80);
           leftView.backgroundColor = [UIColor colorWithRed:0xB9/255.0 green:0xc6/255.0 blue:0xd0/255.0 alpha:1/1.0];
           
           UIView *midView = [[UIView alloc]init];
           midView.frame = CGRectMake(leftView.right + screenW / 375.0 * 10, dateView.bottom + screenW / 375.0 * 8, (screenW - screenW / 375.0 * 10 * 4) * 0.33, screenW / 375.0 * 80);
           midView.backgroundColor = [UIColor colorWithRed:0xB9/255.0 green:0xc6/255.0 blue:0xd0/255.0 alpha:1/1.0];
           
           UIView *rightView = [[UIView alloc]init];
           rightView.frame = CGRectMake(midView.right + screenW / 375.0 * 10, dateView.bottom + screenW / 375.0 * 8, (screenW - screenW / 375.0 * 10 * 4) * 0.33, screenW / 375.0 * 80);
           rightView.backgroundColor = [UIColor colorWithRed:0xB9/255.0 green:0xc6/255.0 blue:0xd0/255.0 alpha:1/1.0];
           
           //根据当天可预约与否，判定整天3个时间段的可预约状况
           
           BPTimeChooseView *innerLeftView = [[BPTimeChooseView alloc]init];
           innerLeftView.tag = 0;
           innerLeftView.delegate = self;
           innerLeftView.frame = CGRectMake(1, 1, leftView.frame.size.width - 2, leftView.frame.size.height - 2);
           self.innerLeftView = innerLeftView;
           
           UILabel *leftLabel = [[UILabel alloc]init];
           self.leftLabel = leftLabel;
           leftLabel.text = @"09:30\n -- \n11:30";
           leftLabel.numberOfLines = 5;
           leftLabel.textAlignment = NSTextAlignmentCenter;
           leftLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
           [leftLabel sizeToFit];
           leftLabel.center = CGPointMake(innerLeftView.frame.size.width * 0.5, innerLeftView.frame.size.height * 0.5);
           [innerLeftView addSubview:leftLabel];
           //           innerLeftView.timeChooseType = TimeChooseTypeChooseable;
           
           BPTimeChooseView *innerMidView = [[BPTimeChooseView alloc]init];
           innerMidView.tag = 1;
           innerMidView.delegate = self;
           innerMidView.frame = CGRectMake(1, 1, leftView.frame.size.width - 2, leftView.frame.size.height - 2);
           self.innerMidView = innerMidView;
           
           UILabel *midLabel = [[UILabel alloc]init];
           self.midLabel = midLabel;
           midLabel.text = @"13:30\n -- \n15:30";
           midLabel.numberOfLines = 5;
           midLabel.textAlignment = NSTextAlignmentCenter;
           midLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
           [midLabel sizeToFit];
           midLabel.center = CGPointMake(innerLeftView.frame.size.width * 0.5, innerLeftView.frame.size.height * 0.5);
           [innerMidView addSubview:midLabel];
           //           innerMidView.timeChooseType = TimeChooseTypeUnchooseable;
           
           BPTimeChooseView *innerRightView = [[BPTimeChooseView alloc]init];
           innerRightView.tag = 2;
           innerRightView.delegate = self;
           innerRightView.frame = CGRectMake(1, 1, leftView.frame.size.width - 2, leftView.frame.size.height - 2);
           self.innerRightView = innerRightView;
           
           UILabel *rightLabel = [[UILabel alloc]init];
           self.rightLabel = rightLabel;
           rightLabel.text = @"15:30\n -- \n17:30";
           rightLabel.numberOfLines = 5;
           rightLabel.textAlignment = NSTextAlignmentCenter;
           rightLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
           
           [rightLabel sizeToFit];
           rightLabel.center = CGPointMake(innerLeftView.frame.size.width * 0.5, innerLeftView.frame.size.height * 0.5);
           [innerRightView addSubview:rightLabel];
           //           innerRightView.timeChooseType = TimeChooseTypeChooseable;
           
           
           [leftView addSubview:innerLeftView];
           [midView addSubview:innerMidView];
           [rightView addSubview:innerRightView];
           
           [chooseTimeView addSubview:leftView];
           [chooseTimeView addSubview:midView];
           [chooseTimeView addSubview:rightView];
           
           //创建多个view 加入到scrollView中去
           for (NSInteger i = 0; i < data.count; i++) {
               //设置明天的时间段可选择情况
               if (i == 1) {
                   if ([data[i][@"state"] integerValue] == 0) {
                       innerLeftView.timeChooseType = TimeChooseTypeUnchooseable;
                       innerMidView.timeChooseType = TimeChooseTypeUnchooseable;
                       innerRightView.timeChooseType = TimeChooseTypeUnchooseable;
                   }
                   else {
                       NSArray *times = data[i][@"times"];
                       NSDateFormatter *format=[[NSDateFormatter alloc]init];
                       [format setDateFormat:@"HH:mm"];
                       //第一个时间段
                       NSDate *startTime1 = [NSDate dateWithTimeIntervalSince1970:[times[0][@"s_time"] doubleValue]];
                       NSString *startTime1String = [format stringFromDate:startTime1];
                       
                       NSDate *endTime1 = [NSDate dateWithTimeIntervalSince1970:[times[0][@"e_time"] doubleValue]];
                       NSString *endTime1String = [format stringFromDate:endTime1];
                       
                       leftLabel.text = [NSString stringWithFormat:@"%@\n -- \n%@", startTime1String, endTime1String];
                       //第二个时间段
                       NSDate *startTime2 = [NSDate dateWithTimeIntervalSince1970:[times[1][@"s_time"] doubleValue]];
                       NSString *startTime2String = [format stringFromDate:startTime2];
                       
                       NSDate *endTime2 = [NSDate dateWithTimeIntervalSince1970:[times[1][@"e_time"] doubleValue]];
                       NSString *endTime2String = [format stringFromDate:endTime2];
                       
                       midLabel.text = [NSString stringWithFormat:@"%@\n -- \n%@", startTime2String, endTime2String];
                       //第三个时间段
                       NSDate *startTime3 = [NSDate dateWithTimeIntervalSince1970:[times[2][@"s_time"] doubleValue]];
                       NSString *startTime3String = [format stringFromDate:startTime3];
                       
                       NSDate *endTime3 = [NSDate dateWithTimeIntervalSince1970:[times[2][@"e_time"] doubleValue]];
                       NSString *endTime3String = [format stringFromDate:endTime3];
                       
                       rightLabel.text = [NSString stringWithFormat:@"%@\n -- \n%@", startTime3String, endTime3String];
                       
                       innerLeftView.timeChooseType = [times[0][@"state"] integerValue] == 0? TimeChooseTypeUnchooseable : TimeChooseTypeChooseable;
                       innerMidView.timeChooseType = [times[1][@"state"] integerValue] == 0? TimeChooseTypeUnchooseable : TimeChooseTypeChooseable;
                       innerRightView.timeChooseType = [times[2][@"state"] integerValue] == 0? TimeChooseTypeUnchooseable : TimeChooseTypeChooseable;
                   }
               }
               
               NSDate *date = [NSDate dateWithTimeIntervalSince1970:[data[i][@"day"] unsignedIntegerValue]];
               NSDateFormatter *format1=[[NSDateFormatter alloc]init];
               [format1 setDateFormat:@"MM/dd"];
               NSString *str1=[format1 stringFromDate:date];
               
               UIView *dateCellView = [[UIView alloc]initWithFrame:CGRectMake(i * screenW / 375.0 * 83, 0, screenW / 375.0 * 83, dateView.frame.size.height)];
               dateCellView.tag = i;
               dateCellView.backgroundColor = [UIColor clearColor];
               //给cellView添加点击事件
               UITapGestureRecognizer *tapOnDateCellView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapOnDateCellView:)];
               [dateCellView addGestureRecognizer:tapOnDateCellView];
               
               
               UILabel *dateLabel = [[UILabel alloc]init];
               
               NSString *prefixDate = [NSString string];
               prefixDate = i == 0 ? @"今天" : i == 1 ? @"明天" : @"后天";
               if (i > 2) {
                   prefixDate = [self weekdayStringFromDate:date];
               }
               
               dateLabel.text = [NSString stringWithFormat:@"%@ %@", prefixDate, str1];
               if (i == 1) {
                   self.selectedDate = dateLabel.text;
               }
               dateLabel.tag = 10;
               dateLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:screenW / 375.0 * 13.0];
               dateLabel.textColor = [UIColor colorWithRed:141/255.0 green:165/255.0 blue:184/255.0 alpha:1/1.0];
               [dateLabel sizeToFit];
               dateLabel.center = CGPointMake(dateCellView.frame.size.width * 0.5, screenW / 375.0 * 10 + dateLabel.frame.size.height * 0.5);
               [dateCellView addSubview:dateLabel];
               
               UILabel *status = [[UILabel alloc]init];
               status.text = data[i][@"stateDesc"];
               status.font = [UIFont fontWithName:@"PingFangSC-Regular" size:screenW / 375.0 * 11.0];
               status.textColor = [UIColor colorWithRed:185/255.0 green:198/255.0 blue:208/255.0 alpha:1/1.0];
               [status sizeToFit];
               status.center = CGPointMake(dateCellView.frame.size.width * 0.5, dateCellView.frame.size.height - screenW / 375.0 * 7 - status.frame.size.height * 0.5);
               [dateCellView addSubview:status];
               if (i == 1) {
                   self.selectedDateCell = dateCellView;
                   [self tapOnDateCellView:tapOnDateCellView];
               }
               
               [dateScrollView addSubview:dateCellView];
           }
           
           [dateView addSubview:dateScrollView];
           
           //分割线
           UIView *separatorLine = [[UIView alloc]init];
           separatorLine.frame = CGRectMake(0, leftView.bottom + screenW / 375.0 * 10, screenW, 0.5);
           separatorLine.backgroundColor = [UIColor colorWithRed:0xd8/255.0 green:0xd8/255.0 blue:0xd8/255.0 alpha:1/1.0];
           [chooseTimeView addSubview:separatorLine];
           
           //底部两个按钮
           UIButton *cancel = [[UIButton alloc]init];
           cancel.backgroundColor = [UIColor colorWithRed:245/255.0 green:166/255.0 blue:35/255.0 alpha:1/1.0];
           cancel.frame = CGRectMake(screenW / 375.0 * 10, separatorLine.bottom + screenW / 375.0 * 10, (screenW - (screenW / 375.0 * 10 * 3)) * 0.5, screenW / 375.0 * 40);
           [cancel setTitle:@"返回" forState:UIControlStateNormal];
           [cancel.titleLabel setFont:[UIFont fontWithName:@"PingFangSC-Medium" size:16]];
           [cancel setTitleColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1/1.0] forState:UIControlStateNormal];
           cancel.layer.cornerRadius = 4.0;
           [chooseTimeView addSubview:cancel];
           
           [cancel addTarget:self action:@selector(cancelChooseTime:) forControlEvents:UIControlEventTouchUpInside];
           
           UIButton *confirm = [[UIButton alloc]init];
           confirm.backgroundColor = [UIColor colorWithRed:118/255.0 green:177/255.0 blue:253/255.0 alpha:1/1.0];
           confirm.frame = CGRectMake(cancel.right + screenW / 375.0 * 10, separatorLine.bottom + screenW / 375.0 * 10, (screenW - (screenW / 375.0 * 10 * 3)) * 0.5, screenW / 375.0 * 40);
           [confirm setTitle:@"确定" forState:UIControlStateNormal];
           [confirm.titleLabel setFont:[UIFont fontWithName:@"PingFangSC-Medium" size:16]];
           [confirm setTitleColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1/1.0] forState:UIControlStateNormal];
           confirm.layer.cornerRadius = 4.0;
           //确定摄影师按钮
           [confirm addTarget:self action:@selector(confirmPhotographer:) forControlEvents:UIControlEventTouchUpInside];
           [chooseTimeView addSubview:confirm];
           
           [self.view addSubview:bgView];
       }
       else {
           [SVProgressHUD showWithStatus:@"请求失败"];
           dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
               [SVProgressHUD dismiss];
           });
       }
   } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       [SVProgressHUD showWithStatus:@"请求失败"];
       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           [SVProgressHUD dismiss];
       });
   }];

}

- (NSString*)weekdayStringFromDate:(NSDate*)inputDate {
    
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周天", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    
    return [weekdays objectAtIndex:theComponents.weekday];
    
}

- (void)registerTime:(NSString *)selectedTime time_id:(NSInteger)time_id {
    //1. 去掉首尾空格和换行符
    selectedTime = [selectedTime stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    //2. 去掉所有空格和换行符
    selectedTime = [selectedTime stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    selectedTime = [selectedTime stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    self.selectedTime = selectedTime;
    self.selectedTimeIndex = time_id;
}

- (void)confirmPhotographer:(UIButton *)confirm {
    
    //拦截未选择时间的情况
    if ([self.selectedTime length] == 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择一个服务时间段" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:ok];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
    }
    else {
        NSString *time_id = self.photographerTimes[self.selectedDateIndex][@"times"][self.selectedTimeIndex][@"id"];
        
        if ([self.delegate respondsToSelector:@selector(selectedPhotographerID:timeID:selectedDate:selectedTime:)]) {
            [self.delegate selectedPhotographerID:self.photographerID timeID:time_id selectedDate:self.selectedDate selectedTime:self.selectedTime];
        }
        [self.navigationController popViewControllerAnimated:YES];
        
    }

}


@end
